from collections.abc import Iterable
import asyncio

try:
    import uvloop

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass


class Scheduler(object):

    def __init__(self, spider):
        self.queue = asyncio.Queue()

    async def add_new_request(self, request):
        """ 将新请求添加到未爬取的集合中(单个请求) """
        if request is None:
            return
        # if request not in self.new_requests and request not in self.old_requests:
        await self.queue.put(request)

    async def add_new_requests(self, requests):
        '''
        将请求添加到队列
        :param request: start_requests中的请求 / 回调函数中的请求
        :return: True(可以不要返回值)
        '''
        if not isinstance(requests, Iterable):
            return
        for request in requests:
            await self.add_new_request(request)

    def __len__(self):
        return self.queue.qsize()

    def has_pending_requests(self):
        '''
        判断队列中是否有请求
        :return:
        '''
        # 如果有请求那么就返回True
        # 没有请求就返回False
        return self.queue.qsize() > 0

    async def new_request(self):
        """ 获取一个未爬取的请求 """
        return await self.queue.get()
        # 提取之后，将其添加到已爬取的链接中
